using System;
using System.IO;
using System.Xml;

namespace PacSnake
{
    /// <summary>
    /// Enum for the different statusses of the game
    /// </summary>
    public enum GameStatus
    {
        NotStarted,
        InProgress,
        SnakeDied,
        LevelFinished,
        WaitingForKey
    }
    
	
	/// <summary>
	/// This class holds the game variables. Some game variables are stored in
	/// a savegame file. These variables are loaded when the game starts. If
	/// it's the first time we start the game, we assign default values.
	/// </summary>
    public class GameData
    {
        // Current level in a level set
        private int currentLevel = 1;
        
        // Status of the game
        private GameStatus gameStatus = GameStatus.NotStarted;

        // The filename of the skin we're using in the game
        private string skinFilename = string.Empty;
        
        // Checks if the game sounds are enabled
        private bool soundsEnabled = true;
        
        // Read current path and remove the 'file:/' from the string
        private string path = Path.GetDirectoryName(System.Reflection.Assembly.
            GetExecutingAssembly().GetName().CodeBase).Substring(6);

        
        #region Constructor
        
        /// <summary>
        /// Constructor: Initializes the savegame
        /// </summary>
        public GameData()
        {
            ReadSavegame();
        }
        
        #endregion
        
        #region Read info from savegame
        
        /// <summary>
        /// Reads the game variables from the savegame. If there's no savegame
        /// we use default values.
        /// </summary>
        public void ReadSavegame()
        {
            string filename = path + "\\savegame.xml";
            XmlDocument doc = new XmlDocument();
            
            if (File.Exists(filename))
            {
                doc.Load(filename);
                skinFilename = doc.SelectSingleNode("//Skin").InnerText;
            }
            else
            {
                skinFilename = path + "\\skins\\ice.xml";
                
                // Create new file savegame.xml
                XmlTextWriter writer = new XmlTextWriter(filename, null);
                writer.Formatting = Formatting.Indented;
                writer.Indentation = 2;
                writer.WriteProcessingInstruction("xml",
                    "version='1.0' encoding='ISO-8859-1'");
                writer.WriteStartElement("savegame");
                writer.Close();
                
                // Load XML document and create all elements
                doc.Load(filename);
                XmlNode root = doc.DocumentElement;
                XmlElement skin = doc.CreateElement("Skin");
                skin.InnerText = path + "\\skins\\ice.xml";
                
                // Add all elements to the root element
                root.AppendChild(skin);
                
                // Save file
                doc.Save(filename);
            }
        }
        
        #endregion
        
        #region Update savegame methods

        /// <summary>
        /// Saves the select skin to the savegame
        /// </summary>
        /// <param name="aSkinFilename">Filename of the skin</param>
        public void SetSkin(string aSkinFilename)
        {
            skinFilename = aSkinFilename;
            string filename = path + "\\savegame.xml";
            
            XmlDocument doc = new XmlDocument();
            doc.Load(filename);
            
            XmlNode nodeSkin = doc.SelectSingleNode("//Skin");
            nodeSkin.InnerText = skinFilename;
            
            doc.Save(filename);
        }
        
        #endregion
        
        #region Properties
        
        public GameStatus GameStatus
        {
            get { return gameStatus; }
            set { gameStatus = value; }
        }
        
        public int CurrentLevel
        {
            get { return currentLevel; }
            set { currentLevel = value; }
        }
        
        public string SkinFilename
        {
            get { return skinFilename; }
        }
        
        public bool SoundsEnabled
        {
            get { return soundsEnabled; }
            set { soundsEnabled = value; }
        }
        
        #endregion
	}
}
